<?php
/* $Id$ */
use Buan\Config;
use Buan\UrlCommand;

$ur = Config::get('ext.Scribble.urlRoot');
ScribbleAdminView::getGlobalView()->addStylesheets("{$ur}/css/scribble/page/edit.css");
ScribbleAdminView::getGlobalView()->addJavascripts("{$ur}/js/scribble/page/edit.js");
?>

<div class="head">
	<h1>Pages</h1>
	<h2><?php $this->html->e($page->isInDatabase() ? 'Edit Page' : 'Creating a new page'); ?></h2>
</div>

<form method="post" action="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'page', $page->isInDatabase() ? $page->id : 'create')); ?>" enctype="multipart/form-data">
	<input type="hidden" name="template" value="<?php $this->html->e("{$outerTemplate->name},{$innerTemplate->name}"); ?>" />

	<div class="std-tabbed">
		<div class="std std-2col">
			<h3><u>G</u>eneral Information</h3>
	
			<div class="col-left">
				<dl class="required">
					<dt>Page name: *</dt>
					<dt class="aid">As shown in the menus on your website</dt>
					<dd>
						<input id="page-name" type="text" class="medium" name="name" value="<?php $this->html->e($page->name); ?>" />
					</dd>
				</dl>

				<?php if(empty($parents)): ?>
					<input type="hidden" name="page_id" value="" />
				<?php else: ?>
					<dl class="required">
						<dt>Parent page: *</dt>
						<dd>
							<select class="medium" name="page_id">
								<option value="">-- No parent --</option>
							<?php foreach($parents as $p): $sel = $p->id==$page->page_id; $ind = $p->_indent*15; ?>
								<option value="<?php echo $p->id; ?>" <?php echo $sel ? 'selected="selected"' : ''; ?> <?php echo $ind>0 ? "style=\"padding-left:{$ind}px;\"" : ''; ?>>
									<?php $this->html->e($p->title); ?>
								</option>
							<?php endforeach; ?>
							</select>
						</dd>
					</dl>
				<?php endif; ?>
	
				<dl class="required">
					<dt>Title: *</dt>
					<dt class="aid">Appears in the &lt;title&gt; tag</dt>
					<dd>
						<input type="text" class="medium" name="title" value="<?php $this->html->e($page->title); ?>" />
					</dd>
				</dl>
	
				<dl>
					<dt>Tags/keywords</dt>
					<dt class="aid">Separate with commas. (<b>a-z, 0-9, -, [space]</b>)</dt>
					<dd>
						<input type="text" class="medium" name="tags" value="<?php $this->html->e(implode(",", $version->getTagsArray())); ?>" />
					</dd>
				</dl>
		
				<dl>
					<dt>Page description</dt>
					<dt class="aid">Provide an overview of the page content</dt>
					<dd>
						<textarea class="medium" name="description" rows="10" cols="40"><?php $this->html->e($version->description); ?></textarea>
					</dd>
				</dl>
			</div>
	
			<div class="col-right">
				<dl id="menuslot-options">
					<dt>In which menus should this page be visible?:</dt>
					<dt class="aid"><a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'help', 'page')); ?>#menu-slots" rel="help">Further help</a></dt>
					<dd>
						<?php $mSlots = Config::get('ext.Scribble.menuSlots'); if(empty($mSlots)): ?>
							<p class="warning">You haven't defined any menus yet, so pages won't appear in your site!</p>
						<?php else: foreach($mSlots as $ms): ?>
							<div><label><input type="checkbox" class="checkbox" name="targetmenus[]" value="<?php $this->html->e($ms); ?>" <?php echo in_array($ms, $version->targetmenus) ? 'checked="checked"' : ''; ?> /> <?php $this->html->e($ms); ?></label></div>
						<?php endforeach; endif; ?>
						<div class="menuslot-option-sub disabled"><label><input type="checkbox" class="checkbox" name="targetmenus[]" value="__submenu__" <?php echo in_array("__submenu__", $version->targetmenus) ? 'checked="checked"' : ''; ?> /> Display as a submenu of the parent page</label></div>
					</dd>
				</dl>

				<dl>
					<dt>Is this the home page?</dt>
					<dd>
						<label><input type="radio" name="isindex" value="1" <?php echo (isset($page->isindex) && $page->isindex==1) || (!isset($page->isindex) && empty($parents)) ? 'checked="checked"' : ''; ?> /> Yes</label> &#160;
						<label><input type="radio" name="isindex" value="0" <?php echo (isset($page->isindex) && $page->isindex!=1) || (!isset($page->isindex) && !empty($parents)) ? 'checked="checked"' : ''; ?> /> No</label>
					</dd>
				</dl>
	
				<dl id="url-options">
					<dt>URL:</dt>
					<dt class="aid">Allows you to specify a <a rel="help" href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'help', 'page')); ?>#custom-url">custom URL</a></dt>
					<dd>
						<label><input type="radio" name="urloption" value="auto" <?php echo $page->customurl=='' ? 'checked="checked"' : ''; ?>/> Auto-generate from page title:</label>
						<input type="text" class="medium" id="auto-url-preview" value="" readonly="readonly" />
					</dd>
					<dd>
						<label><input type="radio" name="urloption" value="relative" <?php echo $page->hasCustomRelativeUrl() ? 'checked="checked"' : ''; ?> /> Use custom relative URL:</label>
						<input type="text" class="medium" name="customurl[relative]" value="<?php $this->html->e($page->hasCustomRelativeUrl() ? $page->customurl : ''); ?>" />
					</dd>
					<dd>
						<label><input type="radio" name="urloption" value="absolute" <?php echo $page->hasCustomAbsoluteUrl() ? 'checked="checked"' : ''; ?>/> Use custom absolute URL:</label>
						/ <input type="text" class="medium" name="customurl[absolute]" value="<?php $this->html->e($page->hasCustomAbsoluteUrl() ? preg_replace("/^\/+/", "", $page->customurl) : ''); ?>" />
					</dd>
				</dl>

				<dl>
					<dt>Handle missing sub paths with this page?</dt>
					<dt class="aid"><a rel"=help" class="help" href="#">More info</a></dt>
					<dd>
						<label><input type="radio" name="handlesubpaths" value="1" <?php echo isset($version->handlesubpaths) && $version->handlesubpaths==1 ? 'checked="checked"' : ''; ?> /> Yes</label> &#160;
						<label><input type="radio" name="handlesubpaths" value="0" <?php echo (isset($version->handlesubpaths) && $version->handlesubpaths!=1) || !isset($page->isindex) ? 'checked="checked"' : ''; ?> /> No</label>
					</dd>
				</dl>
	
				<dl>
					<dt>Publish date</dt>
					<dt class="aid">When should the page go live?</dt>
					<dd>
						<?php if(ScribbleAuth::authorize(ScribbleSession::getUser(), 'page-publish', $page->getPathToSite())): ?>
							<label><input type="radio" name="ispublished" value="0" <?php echo $page->datepublish==0 ? 'checked="checked"' : ''; ?> />Do not publish yet</label>
							<label><input type="radio" name="ispublished" value="1" <?php echo $page->datepublish>0 ? 'checked="checked"' : ''; ?> />Publish on:
							<input id="fe-datepublish" type="text" class="small" name="datepublish" value="<?php echo date("d/m/Y", $page->datepublish>0 ? $page->datepublish : time()); ?>" /></label>
						<?php else: ?>
							<p class="warning">
								You do not have the authority to alter the publish date on this page.
							</p>
						<?php endif; ?>
					</dd>
				</dl>
		
				<dl>
					<dt>Expiry date</dt>
					<dt class="aid">Visitors won't see the page after this date</dt>
					<dd>
						<?php if(ScribbleAuth::authorize(ScribbleSession::getUser(), 'page-publish', $page->getPathToSite())): ?>
							<label><input type="radio" name="doexpire" value="0" <?php echo $page->dateexpire==0 ? 'checked="checked"' : ''; ?> />Never expire once published</label>
							<label><input type="radio" name="doexpire" value="1" <?php echo $page->dateexpire>0 ? 'checked="checked"' : ''; ?> />Expire on:
							<input id="fe-dateexpire" type="text" class="small" name="dateexpire" value="<?php echo date("d/m/Y", $page->dateexpire>0 ? $page->dateexpire : time()+(24*60*60)); ?>" /></label>
						<?php else: ?>
							<p class="warning">
								You do not have the authority to alter the expiry date on this page.
							</p>
						<?php endif; ?>
					</dd>
				</dl>
			</div>
		</div>

		<?php $outerPv = $outerTemplate->getPopulatorView($version); ?>
		<div class="std">
			<h3><?php if($outerPv->getSource()!==NULL): ?>Content (<u>i</u>nner)<?php else: ?><u>C</u>ontent<?php endif; ?></h3>
			<div class="lead">
				<p>
					You are using the <strong><?php $this->html->e($innerTemplate->getTitle()); ?></strong> template
					[<a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'page', $page->id!='' ? $page->id : 'create', array('tchange'=>NULL))); ?>">Change</a>]
				</p>
			</div>
			<div id="content-template-inner">
				<?php echo $innerTemplate->getPopulatorView($version)->render(); ?>
			</div>
		</div>

		<?php if($outerPv->getSource()!==NULL): ?>
			<div class="std">
				<h3>Content (<u>o</u>uter)</h3>
				<div class="lead">
					<p>
						You are using the <b><?php $this->html->e($outerTemplate->getTitle()); ?></b> template.
						[<a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'page', $page->id!='' ? $page->id : 'create', array('tchange'=>NULL))); ?>">Change</a>]
					</p>
				</div>
				<div id="content-template-outer">
					<?php echo $outerPv->render(); ?>
				</div>
			</div>
		<?php endif; ?>

		<div class="std">
			<h3><u>P</u>rivileges</h3>
			<div class="lead">
				<p>The privileges you define here apply to <em>this</em> page only, and override any relevant <em>global</em> privileges you have defined.</p>
			</div>

			<input rel="scribble-widget" scribblewidget:type="privilege-editor" scribblewidget:config="resourceId=<?php echo $page->id; ?>" type="hidden" name="privilege" value="<?php $this->html->e($privilegeData); ?>" />
		</div>

		<?php if($page->isInDatabase()): ?>
			<div class="std">
				<h3><u>R</u>evision History</h3>
				<p>TODO</p>
			</div>
		<?php endif; ?>
	</div>

	<p>
		<button class="submit" type="submit" value="Save">Save</button>
		<?php if($page->isInDatabase()): ?>
			<button class="delete" type="submit" name="method" value="Delete" rel="page-delete" disabled="disabled">Delete</button>
			| <a href="<?php $this->html->e($page->getAbsoluteUrl()); ?>" rel="external">Preview</a>
		<?php endif; ?>
	</p>
</form>